<?php
//模型
class accountingModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'accounting';
		
		$this->totals = array();
		$this->datas = array();
	}
	
	
	function get_list($arr=array(),$page=1,$size=10){
		
		$result = array('list'=>array(),'count'=>0);
		
		
		
		$where = $this->get_wheres($arr);
		
		
		$sql = "select * from ".$this->db->dbprefix.$this->table." where {$where} ORDER BY id DESC limit ".($page-1)*$size.",{$size}";
		
		$query = $this->db->query($sql);
		$list = $query->result_array();
		
		$result['list'] = $list;
		
		
		$sql = "select count(*) as num from ".$this->db->dbprefix.$this->table." where {$where} ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		$count = $row?$row['num']:0;
		
		$result['count'] = $count;
	
		return $result;
	}


	function get_wheres($arr=array()){
		$where = " 1 ";
		if(isset($arr['date_month']) && !empty($arr['date_month'])){
			$where .= " AND date_month ='{$arr['date_month']}'";
		}
		
		if(isset($arr['region']) && !empty($arr['region'])){
			$where .= " AND region ='{$arr['region']}'";
		}
		if(isset($arr['company']) && !empty($arr['company'])){
			$where .= " AND company ='{$arr['company']}'";
		}
		if(isset($arr['bumen']) && !empty($arr['bumen'])){
			$where .= " AND bumen like '%{$arr['bumen']}%'";
		}
		if(isset($arr['subject']) && !empty($arr['subject'])){
			$where .= " AND subject ='{$arr['subject']}'";
		}
		if(isset($arr['bz'])){
			if($arr['bz']=='人民币'){
				$where .= " AND (bz ='{$arr['bz']}' OR bz ='')";
			}
			else{
				$where .= " AND bz ='{$arr['bz']}'";
			}
		}
		if(isset($arr['keyword']) && !empty($arr['keyword'])){
			$where .= " AND info LIKE '%{$arr['keyword']}%'";
		}
		if(isset($arr['user_id']) && !empty($arr['user_id'])){
			$where .= " AND `user_id` ='{$arr['user_id']}'";
		}
		
		if(isset($arr['type']) && !empty($arr['type']) && $arr['type']>=0){
			$where .= " AND `type` ='{$arr['type']}'";
		}
		
		if(isset($arr['start']) && isset($arr['end'])){
			if($arr['start'] == $arr['end']){
				$where .= " AND `date` >='{$arr['start']}' ";
			}else{
				$where .= " AND `date` >='{$arr['start']}' AND `date` <= '{$arr['end']}' ";
			}
		}
		
		if(isset($arr['currentID']) && !empty($arr['currentID'])){
			$where .= " AND `id` < '{$arr['currentID']}' ";
		}
		
		return $where;
	}
	
	function has_data($arr=array()){
		$codes = array();
		foreach($arr as $key=>$val){
			$codes[] = $key.'-'.$val;
		}
		$code = implode('|',$codes);
		
		if(isset($this->datas[$code])){
			return $this->datas[$code];
		}
		
		$where = $this->get_wheres($arr);
		$sql = "select count(id) as num from ".$this->db->dbprefix.$this->table." where {$where} limit 1";
		
		$query = $this->db->query($sql);
		$row = $query->row_array();
		
		$result = $row?$row['num']:0;
		
		$this->datas[$code] = $result;
		return $result;
	}
	
	function get_totals($arr=array(),$field=''){
		$where = $this->get_wheres($arr);
		if($field==''){
			$field_str = "IF(mtype=0,income,expenses) as num";
		}
		else{
			
			$field_str = "IFNULL(SUM({$field}),0) as num";
		}
		$sql = "select {$field_str} from ".$this->db->dbprefix.$this->table." where {$where} limit 1";
		
		if(isset($this->totals[$sql])){
			return $this->totals[$sql];
		}
		
		$query = $this->db->query($sql);
		$row = $query->row_array();
		
		$num = $row?$row['num']:0;
		$this->totals[$sql] = $num;
		
		return $num;
	}
	
	function get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}

	//引入数据
	function yinru_data($arr=array()){
		$where = $this->get_wheres($arr);
		
		$time = time();
		$appends = array(
			'addtime'=>$time
		);
		$base_fields = array('user_id','company','date','date_month','region','bumen','bumen_scale','income','expenses','info','subject','certificate','type','mtype','money','balance','bz');
		
		$select_field = array();
		$insert_field = array();
		
		foreach($base_fields as $val){
			$select_field[] = "`{$val}`";
			$insert_field[] = "`{$val}`";
		}
		
		foreach($appends as $key=>$val){
			$select_field[] = "`{$key}`";
			$insert_field[] = "'{$val}'";
		}
		
		
		//添加了防止重复添加的代码and not exists……
		//$select_sql = "select ".implode(',',$insert_field)." from ".$this->db->dbprefix."cashiers where {$where}";
		$select_sql = "select ".implode(',',$insert_field)." from ".$this->db->dbprefix."cashiers where {$where} and not exists (select certificate from ".$this->db->dbprefix.$this->table." where certificate=".$this->db->dbprefix."cashiers.certificate)";
		
		$insert_sql = "INSERT INTO ".$this->db->dbprefix.$this->table." (".implode(',',$select_field).") {$select_sql} ";
		
		$query = $this->db->query($insert_sql);
		return 1;
	}

	//编辑了对账管理的数据,同步到现金日记账里,ling添加
	function synchro_cash($certificate, $arr=array()){
		$this->db->where('certificate', $certificate);
		return $this->db->update($this->db->dbprefix.'cashiers', $arr);
	}
}

